package rpcx_label_center

import (
	"gobase/rpcx_work_flow"
	"net"
	"strconv"
	"time"
)

type ReqAddRealtimeSnapshot struct {
	Tid                  string                `json:"tid"`
	IdRecTask            int64                 `json:"taskId"`
	App                  string                `json:"app"`
	Stream               string                `json:"stream"`
	SendSnapshotToServer bool                  `json:"sendSnapshotToServer"`
	AllJobs              []JobRealtimeSnapshot `json:"allJobs"`

	DeviceId string `json:"-"` //冗余字段，计算使用
}

type ReqAddHistorySnapshot struct {
	Tid                  string               `json:"tid"`
	IdRecTask            int64                `json:"taskId"`
	App                  string               `json:"app"`
	Stream               string               `json:"stream"`
	SendSnapshotToServer bool                 `json:"sendSnapshotToServer"`
	AllJobs              []JobHistorySnapshot `json:"allJobs"`

	DeviceId string `json:"-"` //冗余字段，计算使用
}

type ReqQuerySnapshotStatus struct {
	Tid string `json:"tid"`
	/**
	 * 如果为空,则返回url对应的最近的groupId
	 */
	IdRecTask int64 `json:"taskId"`
	/**
	 * IdRecTask和app/stream不能同时为空
	 */
	App    string `json:"app"`
	Stream string `json:"stream"`
}

type RespQuerySnapshotStatus struct {

	/**
	 * 200-查询成功并且有job； 204-查询成功但是没有job
	 */
	Code                 int
	Msg                  string
	GroupId              string
	App                  string
	Stream               string
	SendSnapshotToServer bool
	IsRealtime           bool
	/**
	 * 未执行的job列表
	 */
	AllJobsStatus []SnapshotJobStatus
}

type ReqImageList struct {
	IdRecTask int64 `json:"taskId"`
}
type RespImageList struct {
	Code int      `json:"code"`
	Data []string `json:"data"`
}

// type RespAllStreamStat struct {
// 	Code    int32
// 	Msg     string
// 	AllStat []StreamStatResponse
// }

type RespAllStreamStat struct {
	Code int `json:"code"`
	Data []struct {
		App              string `json:"app"`
		Ssrc             string `json:"ssrc"`
		Stream           string `json:"stream"`
		TotalReaderCount int    `json:"totalReaderCount"`
		OpenPortTime     string `json:"openPortTime,omitempty"`
		Port             int    `json:"port,omitempty"`
		StreamType       int    `json:"streamType,omitempty"`
	} `json:"data"`
}

// type StreamStatResponse struct {
// 	Code             int32
// 	Msg              string
// 	TotalReaderCount int32
// 	App              string
// 	Stream           string
// }

type SnapshotJobStatus struct {
	OffsetMillisecond int64
	ImageName         string
	JobTime           JobTimeAbsolute
	ServerInfo        []ServerInfo
}

type JobTimeAbsolute struct {
	Year        int `json:"year"`
	Month       int `json:"month"`
	Day         int `json:"day"`
	Hour        int `json:"hour"`
	Minute      int `json:"minute"`
	Second      int `json:"second"`
	MilliSecond int `json:"milliSecond"`
}

type JobRealtimeSnapshot struct {
	JobTime   JobTimeAbsolute `json:"jobTime"`
	ImageName string          `json:"imageName"`

	ServerInfos []ServerInfo `json:"serverInfos"`

	TimePoint    time.Time                             `json:"-"` //冗余字段，方便调试，值同OffsetMillisecond
	ImageRate    int                                   `json:"-"` //冗余字段，方便调试
	MapSubscribe map[string][]rpcx_work_flow.Subscribe `json:"-"` //冗余字段，计算使用
}

type JobHistorySnapshot struct {
	OffsetMillisecond int64  `json:"offsetMillisecond"`
	ImageName         string `json:"imageName"`

	ServerInfos []ServerInfo `json:"serverInfos"`

	TimePoint    time.Time                             `json:"-"` //冗余字段，计算使用，值同JobTime
	ImageRate    int                                   `json:"-"` //冗余字段，方便调试
	MapSubscribe map[string][]rpcx_work_flow.Subscribe `json:"-"` //冗余字段，计算使用
}

type ServerInfo struct {
	UrlParam    string `json:"serverUrl"` //人工智能服务地址
	PassInfoStr string `json:"otherInfo"` //透传字段

	PassInfo rpcx_work_flow.PassInfo `json:"-"` //冗余字段，计算使用
}

// * ////////////////////////////////////////////////
type TaskHistory struct {
	StructHost            string
	HttpPort              string
	ReqAddHistorySnapshot ReqAddHistorySnapshot
}
type TaskRealtime struct {
	StructHost             string
	HttpPort               string
	ReqAddRealtimeSnapshot ReqAddRealtimeSnapshot
}
type Task struct {
	IdRecTask              int64
	StructHost             string
	HttpPort               string
	TypeSubscribe          int8
	ReqAddRealtimeSnapshot ReqAddRealtimeSnapshot
	ReqAddHistorySnapshot  ReqAddHistorySnapshot
}

func (t *Task) GetTaskLink() string {
	return net.JoinHostPort(t.StructHost, t.HttpPort)
}
func (t *Task) GetImageLink() string {
	port, _ := strconv.Atoi(t.HttpPort)
	return net.JoinHostPort(t.StructHost, strconv.Itoa(port+1))
}
